package cn.zptc.springbootdemo.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：zhaoweiwei
 * @date ：创建于 2025/3/6 14:27
 * @description：
 * @modified By：
 * @version: 1.0.0
 */
@Component
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入了请求处理之前的方法");
        Object user = request.getSession().getAttribute("user");
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/backend") && user==null){
            request.getRequestDispatcher("/backend/login.html").forward(request,response);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("进入了请求处理之后的方法");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("进入了视图渲染之后的方法");
    }
}
